<?php
namespace app\shortvideo\controller;
use app\ApiRest;
use app\Common\LongbingServiceNotice;
use app\shop\model\IndexCardCount;
use app\shortvideo\model\ShortvideoBrowse;
use app\shortvideo\model\ShortvideoVisitors;
use think\App;
use app\shortvideo\model\Shortvideo as model;


class IndexVideo extends ApiRest
{

    protected  $model;

    protected $count_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

        $this->count_model   = new IndexCardCount();



    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 10:08
     * @功能说明:短视频列表页
     */
    public function videoList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //状态开启的
        $dis[] = ['a.status','=',1];
        //查询
        if(!empty($input['video_id'])){

            $dis[] = ['a.id','<>',$input['video_id']];

        }

        $data  = $this->model->indexVideoList($dis,$input['staff_id']);
        //如果通过分享进来
        if(!empty($input['video_id'])){

            $diss[] = ['a.id','=',$input['video_id']];

            $diss[] = ['a.status','=',1];

            $top_video = $this->model->indexVideoList($diss,$input['staff_id'],1);


            if(!empty($data['data'])&&count($data['data'])>=10){

                array_pop($data['data']);
            }

            if(!empty($top_video['data'])){

                array_unshift($data['data'],$top_video['data'][0]);

            }
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 11:06
     * @功能说明:员工端（我的数据）
     */
    public function staffVideoData(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['staff_id','=',$this->getUserId()];

        $dis[] = ['type','=',1];

        $dis[] = ['first','=',1];
        //模型
        $visitors_model   = new ShortvideoVisitors();
        //当天浏览人次
        $data['dayCount']   = $visitors_model->getDayCount($dis);
        //当月浏览人次
        $data['monthCount'] = $visitors_model->getMonthCount($dis);

        $list_dis = [

            'a.uniacid' => $this->_uniacid,

            'a.staff_id'=> $this->getUserId(),

            'b.status'  => 1
        ];
        //视频列表
        $data['list'] = $visitors_model->getVideoList($list_dis,$this->getUserId());

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 14:18
     * @功能说明:获客排行
     */
    public function visitorsTop(){
        //模型
        $visitors_model   = new ShortvideoVisitors();

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //前三位
        $top_three  = $visitors_model->getTopUserLimit($dis,3);

        $data['top_three'] = !empty($top_three)?$top_three:[];
        //列表
        $data['top_list']   = $visitors_model->getTopUserList($dis,10);
        //所有
        $top_list = $visitors_model->getTopUserLimit($dis,99);

        $diss = [];
        //员工姓名
        if(!empty($input['name'])){

            $diss[] = ['a.name','like','%'.$input['name'].'%'];

            $diss[] = ['a.phone','like','%'.$input['name'].'%'];

        }else{

            $dis[]   = ['a.fans_id','=',$this->getUserId()];
        }
        //员工自己
        $self_top = $visitors_model->getTopUserLimit($dis,1,$diss);

        $data['self_top'] = $self_top;

        if(!empty($data['self_top'])){

            $all_staff= array_column($top_list,'fans_id');

            $search_top = array_search($data['self_top'][0]['fans_id'],$all_staff);

            $data['self_top'][0]['top'] = is_numeric($search_top)?$search_top+1:'99+';
        }

        $dis = [

            'uniacid' => $this->_uniacid,

            'type'    => 1

        ];
        //获客总人数
        $data['all_visitors_count'] = $visitors_model->getCount($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 16:09
     * @功能说明:视频数据
     */
    public function videoData(){
        //获客模型
        $visitors_model = new ShortvideoVisitors();
        //浏览模型
        $browse         = new ShortvideoBrowse();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['type','=',1];
        //获客人数
        $data['visitors_people'] = $visitors_model->getCount($dis);
        //获客次数
        $data['visitors_time']   = $visitors_model->getCount($dis,'id');
        array_pop($dis);
        //浏览人数
        $data['view_people']     = $browse->getCount($dis);
        //浏览次数
        $data['view_time']       = $browse->getCount($dis,'id');

        $dis[] = ['mode','=',4];
        //转发人数
        $data['send_people'] = $visitors_model->getCount($dis);
        //转发次数
        $data['send_time']   = $visitors_model->getCount($dis,'id');

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 17:11
     * @功能说明:视频数据列表
     */
    public function videoDataList(){

        $input = $this->_param;

        $v_dis[] = ['a.uniacid','=',$this->_uniacid];

        $v_dis[] = ['a.status','=',1];

        $data    = $this->model->staffVideoList($v_dis,$this->getUserId(),$input['status']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 09:32
     * @功能说明:单个视频数据统计
     */

    public function singeVideoData(){

        $input = $this->_param;

        $dis = [

            'id'     => $input['video_id'],

            'status' => 1
        ];
        //视频详情
        $data['video_info'] = $this->model->videoInfo($dis,'id,cover,title,create_time');

        if(empty($data['video_info'] )){

            $this->errorMsg('视频已下架');

        }
        //浏览时间
        $f_time = datachange( $data['video_info'],'create_time');

        $data['video_info']['create_time'] = $f_time['radar_group'].$f_time['radar_time'];

        //模型
        $model = $this->getData($input)['model'];
        //查询条件
        $dis   = $this->getData($input)['dis'];
        //列表
        $data['list'] = $model->peopleList($dis);
        //次数
        $diss[] = ['video_id','=',$input['video_id']];
        //员工
        $diss[] = ['staff_id','=',$this->getUserId()];
        //今日
        $today = strtotime(date('Y-m-d',time()));

        $tomorrow = strtotime(date('Y-m-d',time()))+86400;
        //今日数据
        if(!empty($input['type'])&&$input['type']==1){

            $diss[] = ['create_time','between',"$today,$tomorrow"];

        }

        $Browse_model   = new ShortvideoBrowse();

        $Visitors_model = new ShortvideoVisitors();
        //浏览次数
        $data['time_data']['browse_count']   = $Browse_model->getCount($diss,'id');

        $map  = $diss;

        $maps = $diss;
        //用户的
        $diss[] = ['type','=',1];
        //获客次数
        $data['time_data']['visitors_count'] = $Visitors_model->getCount($diss,'id');

        $map[] = ['mode','=',4];
        //转发次数
        $data['time_data']['send_count'] = $Visitors_model->getCount($map,'id');

        $maps[] = ['mode','=',5];
        //商品点击次数
        $data['time_data']['goods_count'] = $Visitors_model->getCount($maps,'id');

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 10:25
     * @功能说明:
     */
    public function getData($input){
        //视频
        $dis[] = ['a.video_id','=',$input['video_id']];
        //员工
        $dis[] = ['a.staff_id','=',$this->getUserId()];
        //今日
        $today = strtotime(date('Y-m-d',time()));

        $tomorrow = strtotime(date('Y-m-d',time()))+86400;
        //今日数据
        if(!empty($input['type'])&&$input['type']==1){

            $dis[] = ['a.create_time','between',"$today,$tomorrow"];

        }
        switch ($input['status']){
            //浏览次数
            case 1:
                $model = new ShortvideoBrowse();
                break;
            //转发次数
            case 2:
                $model = new ShortvideoVisitors();

                $dis[] = ['a.mode','=',4];
                break;
            //获客次数
            case 3:
                $model = new ShortvideoVisitors();
                //用户的
                $dis[] = ['a.type','=',1];
                break;
            //商品点击次数
            case 4:
                $model = new ShortvideoVisitors();

                $dis[] = ['a.mode','=',5];
                break;
        }
        //查询条件
        $data['dis']   = $dis;
        //模型
        $data['model'] = $model;

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 11:57
     * @功能说明:我的数据记录
     */
    public function staffVideoList(){

        $input = $this->_param;

        $model = new ShortvideoVisitors();
        //今日
        $today = strtotime(date('Y-m-d',time()));

        $tomorrow = $today+86400;
        //视频
        $dis[] = ['a.video_id','=',$input['video_id']];
        //员工
        $dis[] = ['a.staff_id','=',$this->getUserId()];
        //用户的
        $dis[] = ['a.type','=',1];
        //今日数据
        if(!empty($input['type'])&&$input['type']==1){

            $dis[] = ['a.create_time','between',"$today,$tomorrow"];

        }
        //查询
        $data  = $model->peopleList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-21 11:54
     * @功能说明:我的数据统计详情
     */

    public function staffVisitorsList(){

        $input = $this->_param;

        $model = new ShortvideoVisitors();
        //今日
        $today = strtotime(date('Y-m-d',time()));

        $tomorrow = $today+86400;

        $firstday = date('Y-m-01',time());

        $lastday  = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        //转换成时间戳
        $firstday = strtotime($firstday);

        $lastday  = strtotime($lastday);
        //员工
        $dis[] = ['a.staff_id','=',$this->getUserId()];
        //用户的
        $dis[] = ['a.type','=',1];

        $dis[] = ['a.first','=',1];
        //今日数据
        if(!empty($input['type'])&&$input['type']==1){

            $dis[] = ['a.create_time','between',"$today,$tomorrow"];

        }else{
            //本月
            $dis[] = ['a.create_time','between',"$firstday,$lastday"];

        }
        //查询时间
        if(!empty($input['start_time'])&&$input['end_time']){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //查询
        $data  = $model->peopleList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 13:41
     * @功能说明:添加浏览记录
     */
    public function browseAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['type']    = $this->getUserInfo()['is_staff']==1?2:1;

        $model = new ShortvideoBrowse();

        $res   = $model->browseAdd($input);
        $res = $res?['id'=>$res]:$res;
        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 13:52
     * @功能说明:修改浏览时长
     */
    public function updateBrowseTime(){

        $input = $this->_input;

        $dis = [
            'id' => $input['browse_id'],

            'user_id' => $this->getUserId(),

            'video_id'=> $input['video_id'],

            'browse_time' => 0
        ];

        $model = new ShortvideoBrowse();

        $res   = $model->updateBrowseTime($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 14:05
     * @功能说明:添加获客
     */
    public function visitorsAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['type']    = $this->getUserInfo()['is_staff']==1?2:1;

        $model = new ShortvideoVisitors();

        $res   = $model->visitorsAdd($input);

        switch ($input['mode']){
            //电话号码
            case 2:
                $type = 665;
                break;
            //微信号
            case 3:
                $type = 666;
                break;
            //转发
            case 4:
                $type = 664;
                break;
        }

        if(!empty($type)){
            //上报雷达
            $inset_data = [
                //用户
                'user_id' => $this->getUserId(),
                //员工
                'to_uid'  => $input['staff_id'],
                //类型
                'type'    => $type,
                //标签
                'sign'    => 'view',
                //id
                'target'  => $input['video_id'],
                //uniacid
                'uniacid' => $this->_uniacid
            ];
            //添加
            $insert_id = $this->count_model->countAdd($inset_data);
            //发送
            $push_data = array(
                'action'  => 'longbingSendWxServiceNotice',

                'event'   => 'longbingSendWxServiceNotice',

                'count_id'=> $insert_id
            );

            publisher(json_encode($push_data ,true),5000);

        }

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-21 13:20
     * @功能说明:添加短视频雷达
     */

    public function addVideoRadar(){

        $input = $this->_param;

        if($input['staff_id']==$this->getUserId()){

            return $this->success(false);

        }

        $model = new ShortvideoBrowse();

        $dis = [

            'user_id' => $this->getUserId()
        ];

        $data = $model->browseInfo($dis);

        $type = !empty($data)?663:662;
        //上报雷达
        $inset_data = [
            //用户
            'user_id' => $this->getUserId(),
            //员工
            'to_uid'  => $input['staff_id'],
            //类型
            'type'    => $type,
            //标签
            'sign'    => 'view',
            //id
            'target'  => $input['video_id'],
            //uniacid
            'uniacid' => $this->_uniacid
        ];
        //添加
        $insert_id = $this->count_model->countAdd($inset_data);
        //发送
        $push_data = array(
            'action'  => 'longbingSendWxServiceNotice',

            'event'   => 'longbingSendWxServiceNotice',

            'count_id'=> $insert_id
        );

        publisher(json_encode($push_data ,true));

        return $this->success($insert_id);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-21 09:40
     * @功能说明:员工排行详情
     */
    public function staffTopInfo(){

        $input = $this->_param;
        //头像
        $data['staff_info']['staff_avatar'] = lbUserAvatar($input['staff_id']);
        //昵称
        $data['staff_info']['staff_name']   = lbUserName($input['staff_id']);

        $dis = [

            'staff_id' => $input['staff_id'],

            'type'     => 1

        ];

        $model = new ShortvideoVisitors();
        //获客人数
        $data['staff_info']['visitors_people'] = $model->getCount($dis);

        unset($dis['type']);

        $dis['mode'] = 4;
        //转发次数
        $data['staff_info']['send_count'] = $model->getCount($dis,'id');

        $dis = [

            'a.staff_id' => $input['staff_id'],

            'a.type'     => 1

        ];
        //视频列表
        $data['list'] = $model->getVideoList($dis,$input['staff_id']);

        return $this->success($data);

    }



    /**
     * User: chenniang
     * Date: 2019-09-26 10:32
     * @return void
     * descption:生成活动二维码
     */
    public function getQr(){

        $input = $this->_input;

        $qr    = getCache($this->getUserId().'-'.$input['type'].'-'.$input['staff_id'].'-'.$input['video_id']."-shortvideo",$this->_uniacid);

        if(empty($qr)){

            $input['pid'] = $this->getUserId();

            $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

            $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

            $qr   = $data['qr_path'];

            setCache($this->getUserId().'-'.$input['type'].'-'.$input['staff_id'].'-'.$input['video_id']."-shortvideo",$qr,3600,$this->_uniacid);
        }
        return $this->success($qr);
    }










}
